package result;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回结果
 * @author ZQ
 * @date 2022/7/18 8:55
 */
@Data
public class CommonResult {

    private int code = 200;//状态码

    private boolean flag = true;//状态,用于前端判断是否成功


    /**
     * 返回的结果
     */
    private Object object;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();
    public CommonResult data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public CommonResult data(Map<String, Object> map){
        this.setData(map);
        return this;
    }
    /**
     * 成功返回结果
     * @param object
     * @return
     */
    public static CommonResult ok(Object object){
        CommonResult commonResult = new CommonResult();
        commonResult.setObject(object);
        return commonResult;
    }

    /**
     * 成功返回结果
     * @return
     */
    public static CommonResult ok(){
        return ok(null);
    }

    /**
     * 失败后返回的结果
     * @param object
     * @return
     */
    public static  CommonResult fail(Object object){
        CommonResult commonResult = new CommonResult();
        commonResult.setFlag(false);
        commonResult.setObject(object);
        return commonResult;
    }

    /**
     * 失败后返回的结果
     * @return
     */
    public static  CommonResult fail(){
        return fail(null);
    }


    /**
     * 失败后返回的结果
     * @param object
     * @return
     */
    public static  CommonResult fail(int code,Object object){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(code);
        commonResult.setFlag(false);
        commonResult.setObject(object);
        return commonResult;
    }

}
